const express = require('express')
const router = express.Router()
const pool = require('../pool')

module.exports = router


	//-------------测试----将数据插入数据库------------------	
router.get('/show',(req,res)=>{
	res.send('test')
	console.log('test')
})	
	
//		http://localhost:3000/v10/user/add   地址
	// --------------------------将用户数据插入到数据库中	
router.post('/add',(req,res,next)=>{
	let user = req.body.user
	let uemail = req.body.uemail
	let uphone = req.body.uphone
	let pwd = req.body.pwd
			
	let sql = 'insert into LH_user set user=?,uemail=?,uphone=?,pwd=?'
		
	pool.query(sql,[user,uemail,uphone,pwd],(err,result)=>{
		if(err){
			return next(err)
		}

		if(result.affectedRows===0){
			res.send({code:501,msg:'注册失败'})
		}else{
			res.send({code:200,msg:'注册成功'})
		}	
		
	})
})

//http://localhost:3000/v10/user/list     地址
// --------------------根据查询条件-----------将从数据库中查询的数据显示到页面-------------
//            根据账号，密码，从数据库中取值 
router.get('/login',(req,res,next)=>{
	let uphone = req.query.uphone
	let pwd = req.query.pwd
	
	let sql = 'select uphone,pwd from LH_user where uphone=? and pwd=?'
	pool.query(sql,[uphone,pwd],(err,result)=>{
		if(err){
			return next(err)
		}
		console.log('re1',result)
		if(result.length>0){
			res.send({code:200,msg:'select success 成功',data:result})			
		}else{
			res.send(result)
		}		
	})
})

// -------------------判断手机号是否存在-----------？？

router.get('/phone',(req,res,next)=>{
	let uphone = req.query.uphone
	
	let sql = 'select uphone from LH_user where uphone=?'
	pool.query(sql,[uphone],(err,result)=>{
		if(err){
			return next(err)
		}
		console.log('re1',result)
		if(result.length>0){
			res.send({code:200,msg:'select success 成功',data:result})			
		}else{
			res.send(result)
		}		
	})
})




